package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;
import java.util.HashSet;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.landflag.IBuiltInLandFlag;
import cn.keniykey.mc.landterritory.landflag.LandFlagDescription;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagScheduler;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class BanMoveFlag extends JavaLandFlag implements IBuiltInLandFlag, Listener{
	private String tip;
	
	@Override
	public ELandFlagType getELandFlagType() {
		return ELandFlagType.PLAYER;
	}
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.MUCH;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	@Override
	public LandFlagDescription getBuiltInDescription() {
		HashSet<String> conflict_flags = new HashSet<>();
		conflict_flags.add("BanTpOut");
		return new LandFlagDescription(this.getClass().getName(), "BanMove", (Integer.MAX_VALUE-1), conflict_flags);
	}
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
	}
	
	@EventHandler
	public void onConfigReload(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "禁止在领地内");
			config.set("lore", Arrays.asList("&7（禁止其它玩家在领地内）","&7（玩家会被直接传送到领地外围）","&5（标记参数为许可玩家）"));
			config.set("tip", "&c没有领地许可，不可以在此领地内");
			config.set("tick", 40);
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
		this.tip = LanguageManager.getChatPrefix() + Util.conver(config.getString("tip"));
		final long tick = config.getLong("tick");
		LandFlagScheduler.cancelTask(this);
		LandFlagScheduler.runTaskTimer(this, ()->{
			for(final Player player : Bukkit.getOnlinePlayers()) {
				final Land land = ILandManager.getILandManager().getCircleDetectionLand(player);
				if(land == null || JavaLandFlag.hasLandFlagParameterAllMethod(land, this, player)) continue;
				final Location loc_player = player.getLocation();
				if(!loc_player.getWorld().getName().equals(land.getWorldName())) continue;
				final Location new_loc = loc_player.clone();
				
				final int x = loc_player.getBlockX();
				final int z = loc_player.getBlockZ();
				final int x1 = Math.abs(x - land.getRange().getLandPointOne().getX());
				final int x2 = Math.abs(x - land.getRange().getLandPointTwo().getX());
				final int z1 = Math.abs(z - land.getRange().getLandPointOne().getZ());
				final int z2 = Math.abs(z - land.getRange().getLandPointTwo().getZ());
				final int xz_min = Math.min(Math.min(x1, x2),Math.min(z1, z2));
				if(xz_min == x1) {
					new_loc.setX(land.getRange().getLandPointOne().getX()-0.5d);
				}else if(xz_min == x2) {
					new_loc.setX(land.getRange().getLandPointTwo().getX()+1.5d);
				}else if(xz_min == z1) {
					new_loc.setZ(land.getRange().getLandPointOne().getZ()-0.5d);
				}else if(xz_min == z2) {
					new_loc.setZ(land.getRange().getLandPointTwo().getZ()+1.5d);
				}
				if(player.teleport(new_loc, TeleportCause.PLUGIN)) player.sendMessage(this.tip);
			}
		}, 100l, Math.max(20l, tick));
	}
}